package com.lwj.project1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.lwj.project1.databaseHelper.MySqliteOpenHelper;
import com.lwj.project1.myToast.MyToast;

public class LoginActivity extends AppCompatActivity {
    private ImageButton login_button;
    private Button register_button;
    private Button forget_button;
    private EditText editText_username;
    private EditText editText_password;
    private TextView username_notice;
    private TextView password_notice;
    private String username;
    private String password;
    private ImageView login_head;
    private CheckBox remember;
    private String rem_flag;
    private final MySqliteOpenHelper mySqliteOpenHelper = new MySqliteOpenHelper(LoginActivity.this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        login_button = findViewById(R.id.login_button);
        forget_button = findViewById(R.id.forget);
        login_head = findViewById(R.id.login_head);
        login_head.setImageResource(R.drawable.head);
        register_button = findViewById(R.id.register);
        editText_username = findViewById(R.id.login_username);
        editText_password = findViewById(R.id.login_password);
        username_notice = findViewById(R.id.username_notice);
        password_notice = findViewById(R.id.password_notice);
        remember = findViewById(R.id.remember);
        MyToast myToast = new MyToast(getApplicationContext());

        SharedPreferences sharedPreferences = getSharedPreferences("project_content",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        rem_flag = sharedPreferences.getString("remember","disable");
        editText_username.setText(sharedPreferences.getString("username",""));
        if (rem_flag.equals("enable")) {
            remember.setChecked(true);
            editText_password.setText(sharedPreferences.getString("password",""));
        }else {
            remember.setChecked(false);
            editText_password.setText("");
        }

        editText_username.setOnFocusChangeListener((v, hasFocus) -> {
            username = editText_username.getText().toString();
            if (hasFocus){
                username_notice.setText("");
            }
            else {
                if (username.length() < 6){
                    username_notice.setText("*用户名不能少于6位哦~");
                }
            }
        });
        editText_password.setOnFocusChangeListener((v, hasFocus) -> {
            password = editText_password.getText().toString();
            if (hasFocus){
                password_notice.setText("");
            }
            else {
                if (password.length() < 8){
                    password_notice.setText("*密码不能少于8位哦~");
                }
            }
        });

        forget_button.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this,ForgetPWActivity.class);
            startActivity(intent);
        });
        login_button.setOnClickListener(v -> {
            username = editText_username.getText().toString();
            password = editText_password.getText().toString();
            // 判断不为空
            if (username.equals("") || password.equals("")){
                myToast.showToastWithImg("不要忘记填写账号或密码哦~",getResources().getDrawable(R.drawable.toast_img));
                return;
            }
            if (remember.isChecked()){
                editor.putString("remember","enable");
            }else editor.putString("remember","disable");
            if (!password.equals(mySqliteOpenHelper.findUserByName(username).getPassword())){
                myToast.showToastWithImg("密码不正确哦，请再想想",getResources().getDrawable(R.drawable.toast_img));
                return;
            }
            editor.putString("state","LOGIN");
            editor.putString("username",username);
            editor.putString("password",password);
            editor.apply();
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
        });
        register_button.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
            startActivity(intent);
        });
    }
}